package edu.ubbcs.rs.backend.repository.jpa;

import edu.ubbcs.rs.backend.model.Reservation;
import edu.ubbcs.rs.backend.repository.ReservationRepository;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

/**
 * @author Merli Andras-Bertalan
 * @version 1.0
 * @created 09-jan.-2015 15:37:44
 */
@Stateless(name = "ReservationRepository")
public class JPAReservationRepository extends AbstractJPABaseRepository<Reservation, Long>
        implements
        ReservationRepository {

    @PersistenceContext(unitName = "reservation_system_unit")
    private EntityManager em;
    
    public JPAReservationRepository() {
        super(Reservation.class);
    }

    @Override
    public List<Reservation> getReservationsByDate(Date date) {
        try {
            TypedQuery<Reservation> query = em.createQuery(
                    "SELECT r FROM Reservation r WHERE r.date = :date", Reservation.class);
            query.setParameter("date", date, TemporalType.TIMESTAMP);

            return query.getResultList();
        } catch (final javax.ejb.TransactionRolledbackLocalException ex) {
            ex.printStackTrace();
        }
        return Collections.emptyList();
    }
}
